using System;
using UnityEngine;
using UnityEngine.Serialization;

namespace Lean.Localization
{
	public abstract class LeanLocalizedBehaviour : MonoBehaviour
	{
		[Tooltip("The name of the phrase we want to use for this localized component")]
		[LeanPhraseName]
		[SerializeField]
		[FormerlySerializedAs("PhraseName")]
		private string phraseName;

		public string PhraseName
		{
			get
			{
				return phraseName;
			}
			set
			{
				if (value != phraseName)
				{
					phraseName = value;
					UpdateLocalization();
				}
			}
		}

		public abstract void UpdateTranslation(LeanTranslation translation);

		public void UpdateLocalization()
		{
			UpdateTranslation(LeanLocalization.GetTranslation(phraseName));
		}

		protected virtual void OnEnable()
		{
			LeanLocalization.OnLocalizationChanged = (Action)Delegate.Combine(LeanLocalization.OnLocalizationChanged, new Action(UpdateLocalization));
			UpdateLocalization();
		}

		protected virtual void OnDisable()
		{
			LeanLocalization.OnLocalizationChanged = (Action)Delegate.Remove(LeanLocalization.OnLocalizationChanged, new Action(UpdateLocalization));
		}
	}
}
